package 月度_季度_年度数据;

import Moudle.Excel_class;
import Moudle.Item;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ToExcel {
    static void toExcel(Excel_class[] excel_classes, Item item, String time_code) throws IOException, InterruptedException {
        //写入Excel
        Workbook workbook = new SXSSFWorkbook(10000);
        Sheet sheet = workbook.createSheet();

        Row row = sheet.createRow(0);
        int index = 0;
        row.createCell(index++).setCellValue("指标");
        row.createCell(index++).setCellValue("时间");
        row.createCell(index++).setCellValue("数值");

        int rowNum = 1;
        int count = 0;
        for (Excel_class excel_class : excel_classes){
            Row row1 = sheet.createRow(rowNum++);
            count++;

            row1.createCell(0).setCellValue(excel_class.指标);
            row1.createCell(1).setCellValue(excel_class.时间);
            row1.createCell(2).setCellValue(excel_class.数值);

            //System.out.println(excel_class.指标 + "  " + excel_class.时间 + "  " + excel_class.指标);
        }


//        workbook.write(new FileOutputStream("C:\\Users\\jinghe\\192.168.1.173\\高新技术企业\\excel导出\\高新技术企业\\认定.xlsx"));
        Item temp = item;
        List<String> list = new ArrayList<>();
        while (!temp.pid.equals("")){
            list.add(0, temp.name);
            temp = Main.itemMap.get(temp.pid);
        }
        list.add(0, temp.name);

        String path = "";
        for (String str : list) {
            path = path + "\\" + str;
        }


        String File_path = "";
        switch (time_code){
            case "hgyd":
                File_path = "D:\\测试临时文件\\国家统计局-月度数据\\";
                break;
            case "hgjd":
                File_path = "D:\\测试临时文件\\国家统计局-季度数据\\";
                break;
            case "hgnd":
                File_path = "D:\\测试临时文件\\国家统计局-年度数据\\";
                break;
        }


        File file = new File(File_path + path + ".xls");
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        if(!file.exists()){
            file.createNewFile();
        }
        workbook.write(new FileOutputStream(file));
        System.out.println("写去excel完毕*--------------------");
        System.out.println(file.getAbsolutePath());
        System.out.println("条目数量：" + count + "\n数据数量：" + excel_classes.length);
    }
}
